package com.snow.useviewdemo.function.eventtouch

import android.animation.Animator
import android.animation.ValueAnimator
import android.view.View
import com.snow.useviewdemo.APPApplication
import com.snow.useviewdemo.util.TextViewUtils

/**
 * Created by zhaoyingtao
 * Date: 2021/9/30
 * Describe:关于收缩动画的实现
 */
object AlertAnimUtils {
    private val heightValue: Int = TextViewUtils.init().dp2px(APPApplication.getInstance().applicationContext, 100)
    fun hideAlertView(view: View) {
        val translationY = ValueAnimator.ofFloat(heightValue.toFloat(), 0f)
//        //设置动画
        translationY.duration = 1500
        translationY.addUpdateListener{
            //获取当前的height值
            val h:Float = it.animatedValue as Float
            //动态更新view的高度
            view.layoutParams.height = h.toInt()
            view.requestLayout()
        }
        translationY.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.GONE
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }
        })
        // 设置动画时间
        translationY.start() //启动
    }
    fun showAlertView(view: View) {
        val translationY = ValueAnimator.ofFloat(0f, heightValue.toFloat())
//        //设置动画
        translationY.duration = 1500

        translationY.addUpdateListener{
            //获取当前的height值
            val h:Float = it.animatedValue as Float
            //动态更新view的高度
            view.layoutParams.height = h.toInt()
            view.requestLayout()
        }
        translationY.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {
                view.visibility = View.VISIBLE
            }

            override fun onAnimationEnd(animation: Animator?) {

            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }
        })
        // 设置动画时间
        translationY.start() //启动
    }
}